/**
 * 
 */
package com.risetek.tft.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.risetek.tft.entity.TDict;
import com.risetek.tft.service.DictService;
import com.zlinepay.framework.bean.AjaxJson;
import com.zlinepay.framework.bean.DataGrid;


/**
 * @author  
 * 
 */
@Controller
@RequestMapping("/dict")
public class DictAction extends BaseAction {

	@RequestMapping(value = "/add.action")
	public String add(Integer id, ModelMap model) {
		TDict dto = new TDict();
		if (id != null) {
			dto.setTDict(dictService.findById(id));
		}
		model.addAttribute("dto", dto);
		return "dict/edit";
	}
	
	@RequestMapping(value = "/edit.action")
	public String edit(Integer id, ModelMap model) {
		if (id != null) {
			TDict dto = dictService.findById(id);
			model.addAttribute("dto", dto);
		}
		return "dict/edit";
	}

	/*@RequestMapping(value = "/list.action")
	@ResponseBody
	public List<Map<String, Object>> list(DataGrid dataGrid, Integer id, HttpServletRequest request,
			ModelMap model) {
		StringBuffer hql = new StringBuffer("from TDict t where 1=1");
		if(id == null) {
			hql.append(" and t.TDict is null");
		} else {
			hql.append(" and t.TDict.id="+id);
		}
		hql.append(" order by t.id asc");
		List<TDict> list = dictService.find(hql.toString());
		List<Map<String, Object>> nodes = new ArrayList<Map<String, Object>>();
		for(TDict dict : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", dict.getId());
			map.put("text", dict.getMessage());
			map.put("type", dict.getType());
			map.put("skey", dict.getSkey());
			map.put("state", dict.getTDicts().isEmpty() ? "open" : "closed");
			nodes.add(map);
		}
		return nodes;
	}
*/
	@RequestMapping(value = "/save.action")
	@ResponseBody
	public AjaxJson save(@ModelAttribute(value = "dto") TDict dto,
			BindingResult bindingResult, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		AjaxJson json = new AjaxJson();
		if (dto.getTDict() != null && dto.getTDict().getId() != null) {
		} else {
			dto.setTDict(null);
		}
		if (dto.getId() == null) {
			dictService.save(dto);
		} else {
			TDict persistent = dictService.findById(dto.getId());
			if (persistent != null) {
				BeanUtils.copyProperties(dto, persistent, new String[] { "id", "TDict" });
				dictService.update(persistent);
			}
		}
		json.setSuccess(true);
		json.setMsg("保存成功！");
		dictService.executeProcedure("{call showTreeNodes_dict}");
		return json;
	}

	@RequestMapping(value = "/remove.action")
	@ResponseBody
	public AjaxJson del(Integer[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			dictService.delete(ids);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("删除成功！");
			dictService.executeProcedure("{call showTreeNodes_dict}");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setMsg("删除失败！");
		}
		return ajaxJson;
	}

	@Autowired
	private DictService dictService;

}
